# model.py

class SqlQueryBuilder:
    def __init__(self, tbl, cons, fields=None):
        self.tbl = tbl
        self.cons = cons
        if fields is None:
            fields = [
                "testFormNumber as '委托单号'",
                "sampleModel as '样品型号'",
                "sampleNumber as '样品编号'",
                "testFormItemName as '委托项名称'",
                "equipmentName as '设备名称'",
                "equipmentNumber as '设备编号'",
                "equipmentTWName as '设备台位名称'",
                "testCondition as '检测条件'",
                "varStr1 as '电流'",
                "varStr3 as '排气压力'",
                "varStr5 as '吸气压力'",
                "varStr7 as '排气温度'",
                "varStr9 as '吸气温度'",
                "varStr11 as '壳体温度'",
                "varStr13 as '频率'"
            ]
        self.fields = fields

    def construct_sql(self):
        select_part = ", ".join(self.fields)
        conditions = [f"{key} = '{value}'" for key, value in self.cons.items()]
        sql = f"SELECT {select_part} FROM {self.tbl} WHERE {' AND '.join(conditions)};"
        return sql

    def write_to_file(self, file_path):
        sql_query = self.construct_sql()
        with open(file_path, 'a') as file:
            file.write(sql_query + '\n')